package tp.pr5.gui;

import java.awt.BorderLayout;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.border.TitledBorder;
import javax.swing.table.AbstractTableModel;
import tp.pr5.RobotEngineObserver;
import tp.pr5.items.InventoryObserver;
import tp.pr5.items.Item;


public class RobotPanel extends JPanel implements InventoryObserver,RobotEngineObserver {	
	private JLabel lblFuel; 
	private JLabel lblRec;	
	private TabMod tabData;
	private JTable tbl;

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor
	 */
	public RobotPanel(){
		this.setBorder(new TitledBorder("Robot info"));
		this.setLayout(new BorderLayout());	
		
		init();
	}
	
	/**
	 * builds the components of the interface
	 */
	private void init(){
		lblFuel = new JLabel("0");
		lblRec  = new JLabel("0");	
		
		JPanel panLabels = new JPanel();
		
		/*
		 * Label with fuel value
		 */
		panLabels.add(new JLabel("Fuel: "));
		panLabels.add(lblFuel);
		
		/*
		 * Label with recycled value
		 */
		panLabels.add(new JLabel("Recycled: "));
		panLabels.add(lblRec);
		
		/*
		 * creates the table 
		 */
		tabData = new TabMod();
		tbl = new JTable(tabData);
		//tbl.setEnabled(false);//evita que la tabla sea editable
		
		/*
		 * adds the table and the labels to the layout
		 */
		this.add(tbl, BorderLayout.CENTER);	
		this.add(panLabels,BorderLayout.NORTH);		
		
	}
	
	/**
	 * Returns current selected item in the table
	 */
	public String getSelectedItemId(){
		int rowId = tbl.getSelectedRow();
		if(rowId >= 0)
			return tabData.getValueAt(rowId, 0)+"";
		else
			return null;
	}
	/**
	 * Class for the table
	 * @author Mario
	 *
	 */
	private class TabMod extends AbstractTableModel{
		
		private ArrayList<Item> items;
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		
		private String[] colNames = {"Id","Description"};
		
		public TabMod(){
			this.setData(new ArrayList<Item>());
		}
		
		public void setData(ArrayList<Item> inventory){
			items = inventory;
		}
		
		public String getColumnName(int col){
			return colNames[col];
		}
		
		@Override
		public int getColumnCount() {
			return 2;
		}

		@Override
		public int getRowCount() {			
			return items.size();
		}

		@Override
		public Object getValueAt(int row, int col) {
			Item it = items.get(row);
			if(col == 0){
				return it.getId();
			}else{
				return it.getDescription();
			}		
		}
		
	}
	@Override
	public void inventoryChange(ArrayList<Item> inventory) {		
		tabData.setData(inventory);
		tabData.fireTableDataChanged();
	}

	@Override
	public void inventoryScanned(String inventoryDescription) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void itemEmpty(String itemName) {
		// TODO Buscar el item en el array y eliminarlo
		
	}

	@Override
	public void itemScanned(String description) {
	}

	@Override
	public void communicationCompleted() {
	}

	@Override
	public void communicationHelp(String help) {
	}

	@Override
	public void engineOff(boolean atShip) {
	}

	@Override
	public void raiseError(String msg) {
	}

	@Override
	public void robotSays(String message) {
	}

	@Override
	public void robotUpdate(int fuel, int recycledMaterial) {		
		lblFuel.setText(fuel+"");
		lblRec.setText(recycledMaterial+"");		
	}
}
